<!-- 定时任务记录编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="scheduledTaskLogModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="scheduledTaskLogModalApp.scheduledTaskLog"
        :rules="scheduledTaskLogModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="任务名称:" name="jobName">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.jobName"
                placeholder="请输入任务名称"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="任务分组:" name="jobGroup">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.jobGroup"
                placeholder="请输入任务分组"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="cron表达式:" name="cron">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.cron"
                placeholder="请输入cron表达式"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="接口Bean:" name="beanName">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.beanName"
                placeholder="请输入接口Bean"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="方法:" name="methodName">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.methodName"
                placeholder="请输入方法"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="方法参数:" name="methodParams">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.methodParams"
                placeholder="请输入方法参数"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="执行结果:" name="result">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.result"
                placeholder="请输入执行结果"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="执行开始时间:" name="startTime">
              <a-date-picker
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.startTime"
                placeholder="请选择执行开始时间"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="执行结束时间:" name="endTime">
              <a-date-picker
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.endTime"
                placeholder="请选择执行结束时间"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="失败原因:" name="failReason">
              <a-input
                v-model:value="scheduledTaskLogModalApp.scheduledTaskLog.failReason"
                placeholder="请输入失败原因"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="scheduledTaskLogModalApp.display">
                <teleport to="#scheduledTaskLogEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="scheduledTaskLogListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(scheduledTaskLogModalApp.scheduledTaskLog)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!scheduledTaskLogModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(scheduledTaskLogModalApp.scheduledTaskLog)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {ScheduledTaskLogService} from "@/views/system/scheduledTask/scheduledTaskLog/scheduledTaskLogService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


export default defineComponent({
  components: {},
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let scheduledTaskLogListApp = inject('scheduledTaskLogListApp', reactive({}));
    let scheduledTaskLogGrid = inject('scheduledTaskLogGrid', reactive({}))
    const scheduledTaskLogModalApp = reactive({
      id: null,
      scheduledTaskLog: {},
      isEdit: false,
      display: false
    });
    scheduledTaskLogModalApp.rules = {
      jobName: [
        {
          required: false,
          type: 'string',
          message: '请输入任务名称',
          trigger: 'blur'
        }
      ],
      jobGroup: [
        {
          required: true,
          type: 'string',
          message: '请输入任务分组',
          trigger: 'blur'
        }
      ],
      cron: [
        {
          required: false,
          type: 'string',
          message: '请输入cron表达式',
          trigger: 'blur'
        }
      ],
      beanName: [
        {
          required: false,
          type: 'string',
          message: '请输入接口Bean',
          trigger: 'blur'
        }
      ],
      methodName: [
        {
          required: false,
          type: 'string',
          message: '请输入方法',
          trigger: 'blur'
        }
      ],
      methodParams: [
        {
          required: false,
          type: 'string',
          message: '请输入方法参数',
          trigger: 'blur'
        }
      ],
      result: [
        {
          required: false,
          type: 'string',
          message: '请输入执行结果',
          trigger: 'blur'
        }
      ],
      startTime: [
        {
          required: false,
          type: 'string',
          message: '请输入执行开始时间',
          trigger: 'blur'
        }
      ],
      endTime: [
        {
          required: false,
          type: 'string',
          message: '请输入执行结束时间',
          trigger: 'blur'
        }
      ],
      failReason: [
        {
          required: false,
          type: 'string',
          message: '请输入失败原因',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      scheduledTaskLogModalApp.scheduledTaskLog = reactive({});
      if (routeId) {//路由
        scheduledTaskLogModalApp.id = routeId;
        scheduledTaskLogModalApp.display = false;
        findScheduledTaskLog(scheduledTaskLogModalApp.id);
      } else if (scheduledTaskLogListApp.editModalForEdit) {//修改弹窗
        scheduledTaskLogModalApp.id = scheduledTaskLogListApp.currentId;
        scheduledTaskLogModalApp.display = true;
      } else if (!scheduledTaskLogListApp.editModalForEdit && scheduledTaskLogListApp.addModalForEdit) {//新增弹窗
        scheduledTaskLogModalApp.display = true;
      } else {
        scheduledTaskLogModalApp.title = "新增学生";
      }
      if (scheduledTaskLogModalApp.id) findScheduledTaskLog(scheduledTaskLogModalApp.id);
    })

    const findScheduledTaskLog = (id) => {
      ScheduledTaskLogService.findScheduledTaskLogForView(id).then((res) => {
        scheduledTaskLogModalApp.scheduledTaskLog = res.data;
        //TODO:为编辑准备.
        scheduledTaskLogModalApp.title = "编辑学生 " + scheduledTaskLogModalApp.scheduledTaskLog.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        ScheduledTaskLogService.updateScheduledTaskLog(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        ScheduledTaskLogService.saveScheduledTaskLog(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/system/scheduledTask/scheduledTaskLog`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        scheduledTaskLogGrid.value.commitProxy("query");
      }, 1500);
      scheduledTaskLogListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      scheduledTaskLogListApp,
      scheduledTaskLogModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
